package java_class_work.week08.internet.upimage1;

import java.io.*;
import java.net.Socket;

/**
 * @Author Tiam
 * @Date 2022/11/1 15:27
 * @Description:
 */
public class TcpServerThread1 implements Runnable {
    private Socket socket;

    public TcpServerThread1(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        String ip = socket.getInetAddress().getHostAddress();
        int count = 1;
        File file = new File(".\\src\\java_class_work\\iotest\\" + ip + "(" + count++ + ").jpg");
        try {
            InputStream in = socket.getInputStream();
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len=in.read(bytes))!=-1){
                // 将上传的文件写入
                fileOutputStream.write(bytes,0,len);
            }
            out.println("图片上传成功!");
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
